package controller

import (
	"github.com/gin-gonic/gin"
	log "github.com/micro/go-micro/v2/logger"
	"rpc-service/product/proto/common"
)

type BaseController struct {
}

func (ctrl BaseController) ResponseError(ctx *gin.Context, code int32, message string) {
	ctx.JSON(200, gin.H{"status": code, "message": message})
}

func (ctrl BaseController) ResponseData(ctx *gin.Context, data interface{}) {
	ctx.JSON(200, gin.H{"status": 200, "message": "操作成功", "data": data})
}

func (ctrl BaseController) ResponseMessage(ctx *gin.Context, code int32, message string) {
	ctx.JSON(200, gin.H{"status": code, "message": message, "data": ""})
}

func (ctrl BaseController) ResponseSuccess(ctx *gin.Context) {
	ctx.JSON(200, gin.H{"status": 200, "message": "操作成功", "data": ""})
}

func (ctrl BaseController) Response(ctx *gin.Context, code *common.CodeMessage, data interface{}) {
	if code == nil || code.Code == 0 || code.Code == 200 {
		ctrl.ResponseData(ctx, data)
	} else {
		log.Fatal("RPC Call Error:", code)
		ctrl.ResponseMessage(ctx, code.Code, code.Message)
	}
}
